package com.hzdesk.media.matvector.koreabatch.client.my_paperless.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;


import com.hzdesk.media.matvector.koreabatch.client.my_paperless.config.AppConfig;

/**
 * Created by Administrator on 2018/6/20.
 * 杭州席媒科技有限公司  Developers  刘传华  16
 * 佛祖镇楼 bug辟易
 */

public class SharedPreferencesUtils {

    private static volatile SharedPreferencesUtils mSharedPreferencesUtils = null;
    private static SharedPreferences mSharedPreferences;
    private static SharedPreferences.Editor editor;

    private SharedPreferencesUtils(Context context) {
        mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        editor = mSharedPreferences.edit();
    }

    public static SharedPreferencesUtils getInstance(Context context) {
        if (mSharedPreferencesUtils == null) {
            synchronized (SharedPreferencesUtils.class) {
                if (mSharedPreferencesUtils == null) {
                    mSharedPreferencesUtils = new SharedPreferencesUtils(context);
                }
            }
        }
        return mSharedPreferencesUtils;
    }

    /**
     * @desc 向sp中添加数据
     */
    public String putDate(String key, Object obj) {
        // key 不为null时再存入，否则不存储
        if (key != null) {
            if (obj instanceof Integer) {
                editor.putInt(key, (Integer) obj);
            } else if (obj instanceof Long) {
                editor.putLong(key, (Long) obj);
            } else if (obj instanceof Boolean) {
                editor.putBoolean(key, (Boolean) obj);
            } else if (obj instanceof Float) {
                editor.putFloat(key, (Float) obj);
            } else if (obj instanceof String) {
                editor.putString(key, (String) obj);
            }
            editor.apply();
        }
        return key;
    }

    /**
     * @desc 根据key 获取对应sp的值
     */
    public Object getDate(String key, String type) {
        switch (type) {
            case "String":
                return mSharedPreferences.getString(key, "");
            case "Integer":
                return mSharedPreferences.getInt(key, 0);
            case "Boolean":
                return mSharedPreferences.getBoolean(key, false);
            case "Float":
                return mSharedPreferences.getFloat(key, 0);
            case "Long":
                return mSharedPreferences.getLong(key, 0);
        }
        return null;
    }

    /**
     * @desc 清空sp中的数据
     */
    public void clear() {
        editor.clear().commit();
    }

    public void clearKey(String key) {
        editor.remove(key);
        editor.commit();
    }

    /**
     *判断首运行
     * 默认：是
     */
    public static boolean isFirstRun(Context context) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        boolean isFirstRun = sp.getBoolean(AppConfig.IS_FIRST_RUN, true);
        return isFirstRun;
    }

    /**
     * 仅在WI-FI下下载
     * 默认：是
     * @return
     */
    public static boolean downloadOnlyInWireless(Context context){
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        boolean isOnly=sp.getBoolean(AppConfig.DOWNLOAD_ONLY_IN_WIRELESS,true);
        return isOnly;
    }




}
